package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.dao.mapper.BamuSpReconciliationSettlementOrderMapper;
import com.yunyao.dao.model.BamuSpReconciliationSettlementOrder;
import com.yunyao.dao.service.bamu.IBamuSpReconciliationSettlementOrderService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供应商对账结算订单表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuSpReconciliationSettlementOrderServiceImpl extends ServiceImpl<BamuSpReconciliationSettlementOrderMapper, BamuSpReconciliationSettlementOrder> implements IBamuSpReconciliationSettlementOrderService {

    /**
     * 根据对账ID列表查询已标记为未删除的对账订单列表
     * 此方法用于检索被标记为未删除的对账订单，确保只操作有效的对账记录
     *
     * @param reconciliationIdList 对账ID列表，用于筛选目标对账订单
     * @return 返回未删除的对账订单列表如果输入列表为空，返回空列表
     */
    @Override
    public List<BamuSpReconciliationSettlementOrder> listByIdsDeleted(List<String> reconciliationIdList) {
        // 检查对账ID列表是否为空，如果为空则直接返回空列表
        if (CollectionUtils.isEmpty(reconciliationIdList)){
            return List.of();
        }
        // 构建查询条件，包括对账ID在给定列表中且未被标记为删除
        LambdaQueryWrapper<BamuSpReconciliationSettlementOrder> wrapper =
                new LambdaQueryWrapper<BamuSpReconciliationSettlementOrder>()
                        .in(BamuSpReconciliationSettlementOrder::getReconciliationId,
                                reconciliationIdList)
                        .eq(BamuSpReconciliationSettlementOrder::getIsDeleted,
                                DataDeletedStatus.NO.getType());
        // 使用构建的查询条件查询对账订单列表
        return list(wrapper);
    }

    @Override
    public List<BamuSpReconciliationSettlementOrder> listByReconciliationSettlementIdDeleted(String reconciliationSettlementId) {
        // 检查对账ID列表是否为空，如果为空则直接返回空列表
        if (StringUtils.isBlank(reconciliationSettlementId)) {
            return List.of();
        }

        // 创建查询条件封装Lambda表达式，用于筛选特定条件的对账结算单
        LambdaQueryWrapper<BamuSpReconciliationSettlementOrder> wrapper =
                new LambdaQueryWrapper<BamuSpReconciliationSettlementOrder>()
                        // 条件1：对账ID在给定的列表中
                        .in(BamuSpReconciliationSettlementOrder::getReconciliationSettlementId,
                                reconciliationSettlementId)
                        // 条件2：对账结算单的删除状态为未删除
                        .eq(BamuSpReconciliationSettlementOrder::getIsDeleted,
                                DataDeletedStatus.NO.getType());
        return list(wrapper);
    }

    @Override
    public void deletedByReconciliationIdList(List<String> reconciliationIdList) {
        if (CollectionUtils.isEmpty(reconciliationIdList)) {
            return;
        }
        LambdaUpdateWrapper<BamuSpReconciliationSettlementOrder> wrapper =
                new LambdaUpdateWrapper<BamuSpReconciliationSettlementOrder>()
                        .in(BamuSpReconciliationSettlementOrder::getReconciliationId,
                                reconciliationIdList)
                        .eq(BamuSpReconciliationSettlementOrder::getIsDeleted,
                                DataDeletedStatus.NO.getType())
                        .set(BamuSpReconciliationSettlementOrder::getIsDeleted,
                                DataDeletedStatus.YES.getType());
        update(wrapper);
    }
}
